<template>
  <!-- 车位使用率走势 -->
  <div class="chart-box2">
    <div class="chart-title w-100p">
      <a-row class="w-100p" type="flex" align="middle" justify="space-between">
        <a-col>车位使用率走势</a-col>
        <a-col>
          <small-tab
            :tabList="tabList"
            :activeKey="activeTab"
            @change="handleTabChange"
          />
        </a-col>
      </a-row>
    </div>
    <div class="chart-content">
      <div class="chart-container" :id="chartId"></div>
    </div>
  </div>
</template>

<script>
import {
  defineComponent,
  onMounted,
  reactive,
  toRefs,
  onBeforeUnmount,
  nextTick,
  watch,
} from "vue";
import { Chart, registerTheme } from "@antv/g2";
import common from "../../utils/common";
import SmallTab from "../common/SmallTab.vue";
import store from "../../../../store";

export default defineComponent({
  name: "ParkingLotUseTrend",
  components: { SmallTab },
  setup() {
    const state = reactive({
      chartId: "parkingLotUseTrendChart",
      chart: null,
      activeTab: "day",
      tabList: [
        { key: "hour", title: "近12小时" },
        { key: "day", title: "近1天" },
      ],
      data: {
        day: [],
        hour: [],
      },
      list: [],
    });

    const handleTabChange = (e) => {
      state.activeTab = e.key;
      state.list = state.data[state.activeTab];
      state.chart.changeData(state.list);
    };

    const initChart = () => {
      const data = state.list;
      state.chart = new Chart({
        container: state.chartId,
        autoFit: true,
        theme: "chartTheme",
      });
      state.chart.data(data);

      state.chart.scale("value", {
        nice: true,
        alias: "车位使用率",
        min: 0,
        max: 100,
      });
      state.chart.axis("value", {
        label: {
          formatter: (text) => {
            return text.replace(/(\d)(?=(?:\d{3})+$)/g, "$1,");
          },
        },
      });

      state.chart.line().position("time*value");
      state.chart.area().position("time*value");
      state.chart.render();
    };

    watch(
      () => store.getters.centerControl.Park_parkSpaceTreadList,
      (val) => {
        if (val) {
          getData();
        }
      }
    );

    const getData = () => {
      const { Park_parkSpaceTreadList } = store.getters.centerControl;
      if (Park_parkSpaceTreadList) {
        state.data = Park_parkSpaceTreadList;
        state.list = Park_parkSpaceTreadList.day;
        if (state.chart) {
          state.chart.changeData(state.list);
        } else {
          nextTick(() => {
            initChart();
          });
        }
      }
    };

    onMounted(() => {
      registerTheme("chartTheme", common.chartTheme);
      getData();
    });

    onBeforeUnmount(() => {
      const chartBox = document.getElementById(state.chartId);
      if (chartBox) {
        chartBox.innerHTML = "";
      }
    });
    return {
      ...toRefs(state),
      handleTabChange,
    };
  },
});
</script>

<style lang="less" scoped>
@import "../../styles/common.less";
</style>
